﻿using System;
using System.IO;
using System.Xml;
using System.Net;

using Microsoft.XLANGs.BaseTypes;
using Microsoft.BizTalk.Message.Interop;
using Microsoft.Samples.BizTalk.Adapter.Common;
using Sixeyed.CacheAdapter.Management;
using Sixeyed.CacheAdapter.Logging;

namespace Sixeyed.CacheAdapter.Runtime
{
    /// <summary>
    /// Send port properties for static configuration
    /// </summary>
    public class CacheTransmitProperties : ConfigProperties
    {
        private static string protocolAlias = "cache://";
        private static int batchSize = 50;
        private static PropertyBase isSolicitResponseProp = new BTS.IsSolicitResponse();

        private bool isTwoWay;		
        private string uri;
        private TransmitLocation _transmitLocation;

        /// <summary>
        /// Gets the TransmitLocation configuration for the port
        /// </summary>
        public TransmitLocation TransmitLocation
        {
            get { return _transmitLocation; }
        }        

        /// <summary>
        /// Constructor with known state
        /// </summary>
        /// <param name="message">Message</param>
        /// <param name="propertyNamespace">Namespace</param>
        public CacheTransmitProperties(IBaseMessage message, string propertyNamespace)
        {
            //read adapter configuration:
            string config = (string) message.Context.Read("AdapterConfig", propertyNamespace);
            this.isTwoWay = (bool) message.Context.Read(isSolicitResponseProp.Name.Name, isSolicitResponseProp.Name.Namespace);

            //currently only deaL with static ports:
            if (!string.IsNullOrEmpty(config))
            {
                _transmitLocation = TransmitLocation.Deserialize(config);
            }
            else
            {
                Logger.Log(LogLevel.Error, "CacheTransmitProperties - dynamic ports not supported.");
            }
        }

        /// <summary>
        /// Constructor with known state
        /// </summary>
        /// <param name="uri">Uri</param>
        public CacheTransmitProperties(string uri)
        {
            this.uri = uri;
            UpdateUriForDynamicSend();
        }
		
		/// <summary>
		/// Returns the batch size of the handler
		/// </summary>
		public static int BatchSize	
        { 
            get { return CacheTransmitProperties.batchSize; }	
        }

		/// <summary>
		/// Returns whether the port is two-way
		/// </summary>
        public bool IsTwoWay 
        { 
            get { return this.isTwoWay; } 
        }

        /// <summary>
        /// Returns the Uri of the port
        /// </summary>
        public string Uri 
        { 
            get { return this.uri; } 
        }

        public static void TransmitHandlerConfiguration(XmlDocument configDOM) {}

        public void UpdateUriForDynamicSend()
        {
            // Strip off the adapters alias
            if ( this.uri.StartsWith(protocolAlias, StringComparison.OrdinalIgnoreCase))
            {
                string newUri = this.uri.Substring(protocolAlias.Length);
                this.uri = newUri;
            }
        }
    }
}
